import time
import zmq
from kvsimple import KVMsg


def main():
    ctx = zmq.Context()
    snapshot = ctx.socket(zmq.DEALER)
    snapshot.linger = 0
    snapshot.connect("tcp://localhost:5556")
    subscriber = ctx.socket(zmq.SUB)
    subscriber.linger = 0
    subscriber.setsockopt(zmq.SUBSCRIBE, b'')
    subscriber.connect("tcp://localhost:5557")

    kvmap = {}

    sequence = 0
    snapshot.send(b"ICANHAZ?")
    while True:
        try:
            kvmsg = KVMsg.recv(snapshot)
        except KeyboardInterrupt:
            break

        if kvmsg.key == "KTHXBAI":
            sequence = kvmsg.sequence
            print("Received snapshot=%d" % sequence)
            break
        kvmsg.store(kvmap)

    while True:
        try:
            kvmsg = KVMsg.recv(subscriber)
        except KeyboardInterrupt:
            break
        if kvmsg.sequence > sequence:
            sequence = kvmsg.sequence
            kvmsg.store(kvmap)


if __name__ == '__main__':
    main()
